import { IssuesRemind } from './issuesRemind';

IssuesRemind.StartIssuesRemindCheck();

chrome.runtime.onInstalled.addListener(() => {
    chrome.storage.sync.set({ color: '#3aa757' }, () => {
        console.log("The color is green.");
    });

    chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
        chrome.declarativeContent.onPageChanged.addRules([{
            conditions: [new chrome.declarativeContent.PageStateMatcher({
                pageUrl: { hostEquals: 'developer.chrome.com' },
            })
            ],
            actions: [new chrome.declarativeContent.ShowPageAction()]
        }]);
    });
});

let gitlabUrl = '';

chrome.webRequest.onCompleted.addListener((details) => {
    let requestUrl = details.url.toLowerCase();

    if (requestUrl.indexOf(gitlabUrl.toLowerCase()) < 0) {
        return;
    }

    console.log(requestUrl);
    // console.log(details.url);
    // if (details.url.indexOf('.m3u8') > 0) {
    //     console.error(details.url);
    // }
    // console.log(details);
}, {
    urls: [
        "<all_urls>"
    ],
    types: ["main_frame"]
},
    [
        "responseHeaders"
    ]
);

// ========================================================================================================== 
let methodHandler: any = {};
let msgType = "bg";
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    let cmdType = request.cmdType;
    let argus = request.argus || [];
    argus.push(sendResponse);

    if (cmdType.indexOf(msgType + ".") !== 0) {
        return;
    }

    let innerCmdType = cmdType.substr(msgType.length + 1);

    methodHandler[innerCmdType].apply(methodHandler, argus);
});

methodHandler.getVideoSrc = (cb) => {
    let src = location.href; //  document.querySelector(".txp_video_container.txp_video_fit_cover video[src]").src;
    cb({ videoSrc: src });
};

setInterval(() => {
    try {
        chrome.storage.sync.get('gitlabConfig', (config) => {
            let data = config.gitlabConfig;
            gitlabUrl = data.gitlabUrl || '';
            // domInputGitlabToken.value = data.gitlabToken || '';
        });
    } catch (e) {

    }
}, 2000);
